<?php

class BlexportsController extends AppController {

	var $name = 'Blexports';
	var $helpers = array('Html', 'Form', 'Csv');

	function beforeRender() {
		$groupid = $this->Auth->user('group_id');
		$name = 'Export';
		IF($groupid != 1){
			$result1 = mysql_query("SELECT id FROM applications WHERE name = '$name'");
			$appid = mysql_fetch_object($result1)->id;
			$result2 = mysql_query("SELECT count(*) as count FROM groupshasrights WHERE group_id = '$groupid' AND application_id = '$appid'");
			$count = mysql_fetch_object($result2)->count;
			IF($count == 0){
				$this->Session->setFlash(__('Access Denied', true));
				$this->redirect(array('controller' => 'pages' ,'action' => 'error'));	
			}
		}
	}
	
	function index(){
		$result = mysql_query("SELECT id, name, description FROM projects");
    	$this->set(compact('result'));	
	}
	
	function export(){
		$pid = $this->params['url']['pid'];
    	$this->set(compact('pid'));
	}
	
	
	function export1(){
		$this->layout = null;
        $this->autoLayout = false;	
        $pid = $this->params['url']['pid'];
        $result = mysql_query("SELECT c.account_number, c.inst_first_name, c.inst_last_name, c.salutation, c.company_name, c.po_box, c.language, c.bill_street, c.bill_house_no, c.bill_zip, c.bill_city, m.mac, mt.description, b.id, mst.name FROM macs AS m, mactypes AS mt, runshasmacs AS rhm, customers AS c, customershasmacs AS chm, blacklists AS b, macservicetypes AS mst, runs AS r WHERE m.id = rhm.mac_id AND m.mactype_id = mt.id AND c.id = chm.customer_id AND m.id = chm.mac_id AND b.mac_id = m.id AND b.letter = 1 AND m.macservicetype_id = mst.id AND rhm.run_id = r.id AND r.project_id = '$pid'");
    	$this->set(compact('result','pid')); 
	}
	
	function export2(){
		$this->layout = null;
        $this->autoLayout = false;	
        $pid = $this->params['url']['pid'];
        $result = mysql_query("SELECT m.mac, b.id FROM macs AS m, mactypes AS mt, runshasmacs AS rhm, customers AS c, customershasmacs AS chm, blacklists AS b, runs AS r WHERE m.id = rhm.mac_id AND m.mactype_id = mt.id AND c.id = chm.customer_id AND m.id = chm.mac_id AND b.mac_id = m.id AND b.ctsp = 1 AND rhm.run_id = r.id AND r.project_id = '$pid'");
    	$this->set(compact('result','pid')); 
	}
	
	function export3(){
		$this->layout = null;
        $this->autoLayout = false;	
        $pid = $this->params['url']['pid'];
        $result = mysql_query("SELECT c.account_number, b.id FROM macs AS m, mactypes AS mt, runshasmacs AS rhm, customers AS c, customershasmacs AS chm, blacklists AS b, runs AS r WHERE m.id = rhm.mac_id AND m.mactype_id = mt.id AND c.id = chm.customer_id AND m.id = chm.mac_id AND b.mac_id = m.id AND b.interaction = 1 AND rhm.run_id = r.id AND r.project_id = '$pid'");
        $this->set(compact('result','pid')); 
	}
}
?>
